<?php

namespace App\Services;

use App\Models\GameChannel;
use App\Models\GameChannelGoodsId;

class GameChannelService extends ServiceBase
{
    private $cacheList = [];

    // 获取某款游戏某个渠道的配置信息
    public function get($id, $get_object = false)
    {
        if (isset($this->cacheList[$id])) {
            $game_channel = $this->cacheList[$id];
        } else {
            $game_channel = GameChannel::find($id);
            $this->cacheList[$id] = $game_channel;
        }

        $game_channel = GameChannel::find($id);
        if (!$get_object) {
            return $game_channel->toArray();
        } else {
            return $game_channel;
        }
    }

    public function getGoodsId($game_id, $channel, $goods_id)
    {
        return GameChannelGoodsId::whereRaw("game_id={$game_id} AND channel={$channel} AND goods_id='{$goods_id}'")->first();
    }

    // 检测和获取渠道配置id
    public function checkAndGetId($game_id, $channel, $id = 0)
    {
        if ($id > 0) {
            $config = GameChannel::whereRaw("id={$id} AND game_id={$game_id}")->first();
            if ($config) {
                return $id;
            }
        }

        $config = GameChannel::whereRaw("game_id={$game_id} AND channel={$channel}")->first();
        return $config->id;
    }

    // 检测和获取渠道配置
    public function checkAndGet($game_id, $channel, $id = 0)
    {
        if ($id > 0) {
            $config = GameChannel::whereRaw("id={$id} AND game_id={$game_id}")->first();
            if ($config) {
                return $config->toArray();
            }
        }

        $config = GameChannel::whereRaw("game_id={$game_id} AND channel={$channel}")->first();
        return $config->toArray();
    }
}
